package com.yangchoi.jetpackcompose.ui.main.page

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemsIndexed
import com.google.accompanist.flowlayout.FlowRow
import com.yangchoi.jetpackcompose.route.RouteKey
import com.yangchoi.jetpackcompose.route.RouteNavigationUtil
import com.yangchoi.jetpackcompose.ui.baseui.BaseComposePage
import com.yangchoi.jetpackcompose.ui.main.viewmodel.HomeViewEvent
import com.yangchoi.jetpackcompose.ui.main.viewmodel.HomeViewModel
import com.yangchoi.jetpackcompose.ui.widget.component.BannerItem
import com.yangchoi.jetpackcompose.ui.widget.component.BannerWidget
import com.yangchoi.jetpackcompose.ui.widget.component.CommonTopBar
import com.yangchoi.jetpackcompose.ui.widget.component.Paging3RefreshWidget
import com.yangchoi.jetpackcompose.ui.widget.component.TestImgUrl
import com.yangchoi.jetpackcompose.ui.widget.ext.onClick
import com.yangchoi.jetpackcompose.utils.Base64Util
import com.yangchoi.lib_base.network.entity.ArticleEntity

@Composable
fun HomePage(navHostController: NavHostController,viewModel:HomeViewModel = hiltViewModel()){

    val viewState = viewModel.viewState
    val bannerList = viewState.bannerList
    val articleDataList = viewState.pagingData?.collectAsLazyPagingItems()

    BaseComposePage(
        bgColor = Color(0xffc5c5c5),
        onResume = {
            viewModel.dispatch(event = HomeViewEvent.OnInitHomeData)
        }
    ) {

        CommonTopBar(title = "首页", showBackIcon = false)

        bannerList?.let {
            Box(modifier = Modifier.padding(start = 12.dp, end = 12.dp, top = 12.dp)){
                BannerWidget(dataList = bannerList) { index,item ->
                    BannerItem(item = item.imagePath, desc = item.title, index = index)
                }
            }
        }

        articleDataList?.let {
            Paging3RefreshWidget(lazyPagingItems = articleDataList){
                itemsIndexed(articleDataList){ index, value ->
                    HomeArticleItem(item = value!!){ item ->
                        RouteNavigationUtil.navigationTo(navHostController,RouteKey.WEB_VIEW.toString(), args = Base64Util.stringToBase64("${item.link}"))
                    }
                }
            }
        }

    }
}

@Composable
fun HomeArticleItem(item:ArticleEntity,onItemClick:(ArticleEntity) ->Unit){
    Column(modifier = Modifier
        .padding(start = 12.dp, top = 12.dp, end = 12.dp)
        .fillMaxWidth()
        .background(color = Color.White, shape = RoundedCornerShape(12.dp))
        .onClick {
            onItemClick.invoke(item)
        }) {
        Text(text = "${item.title}", style = TextStyle(color = Color.Black, fontSize = 14.sp), modifier = Modifier.padding(start = 10.dp, top = 10.dp), maxLines = 2, overflow = TextOverflow.Ellipsis)
        Row(modifier = Modifier
            .padding(bottom = 10.dp)
            .fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
            Text(text = "作者:${item.author ?: "洪洋"}", style = TextStyle(color = Color.Black, fontSize = 12.sp), modifier = Modifier.padding(start = 10.dp, top = 10.dp), maxLines = 2, overflow = TextOverflow.Ellipsis)
            Text(text = "分享于:${item.niceDate}", style = TextStyle(color = Color(0xffc5c5c5), fontSize = 12.sp), modifier = Modifier.padding(end = 10.dp, top = 10.dp), maxLines = 2, overflow = TextOverflow.Ellipsis)
        }
        item.tags?.let {
            FlowRow(modifier = Modifier
                .padding(start = 10.dp, end = 10.dp, bottom = 10.dp)
                .fillMaxWidth()) {
                item.tags?.forEach { it->
                    Box(
                        modifier = Modifier.background(
                            color = Color.Blue,
                            shape = RoundedCornerShape(8.dp)
                        )
                    ){
                        Text(text = "${it.name}", style = TextStyle(color = Color.White, fontSize = 10.sp), modifier = Modifier.padding(horizontal = 10.dp, vertical = 3.dp))
                    }
                }
            }
        }
    }
}